#define PLUGIN_VERSION "2.0"
#pragma semicolon 1

#pragma newdecls required
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>

ConVar g_hCvarColor_tank, g_hCvarColor_witch, g_hCvarColor_tankstone;
int g_iCvarColor_tank, g_iCvarColor_witch, g_iCvarColor_tankstone;

public Plugin myinfo =  
{
	name = "[L4D2]tank,witch,tank石头轮廓发光",  
	author = "木白为柏(axyio)",   
	description = "轮廓发光",             
	version = PLUGIN_VERSION,              
	url = ""                     
}

public void OnPluginStart() 
{
	CreateConVar("l4d2_border_color_version", PLUGIN_VERSION, "l4d2_border_color_version", FCVAR_NOTIFY);
	g_hCvarColor_tank	   = CreateConVar("l4d2_border_color_tank", "139 58 58", "The default tank color. Three values between 0-255 separated by spaces. RGB Color255 - Red Green Blue. (white recommend) 79 79 79/(red recommend) 139 58 58.(empty=disable)", FCVAR_NOTIFY);
	g_hCvarColor_witch	   = CreateConVar("l4d2_border_color_witch", "79 79 79", "The default witch color. Three values between 0-255 separated by spaces. RGB Color255 - Red Green Blue. (white recommend) 79 79 79/(red recommend) 139 58 58.(empty=disable)", FCVAR_NOTIFY);
	g_hCvarColor_tankstone	   = CreateConVar("l4d2_border_color_tankstone", "79 79 79", "The default tankstone color. Three values between 0-255 separated by spaces. RGB Color255 - Red Green Blue. (white recommend) 79 79 79/(red recommend) 139 58 58.(empty=disable)", FCVAR_NOTIFY);

	g_hCvarColor_tank.AddChangeHook(SConVardonChanged);
	g_hCvarColor_witch.AddChangeHook(SConVardonChanged);
	g_hCvarColor_tankstone.AddChangeHook(SConVardonChanged);

	AutoExecConfig(true, "l4d2_border_color");
}

public void OnMapStart()
{	
	init();
}

public void SConVardonChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
	init();
}

void init()
{
	char sColorTank[1024];
	g_hCvarColor_tank.GetString(sColorTank, sizeof(sColorTank));
	g_iCvarColor_tank = GetColor(sColorTank);

	char sColorWitch[1024];
	g_hCvarColor_witch.GetString(sColorWitch, sizeof(sColorWitch));
	g_iCvarColor_witch = GetColor(sColorWitch);

	char sColorTankstone[1024];
	g_hCvarColor_tankstone.GetString(sColorTankstone, sizeof(sColorTankstone));
	g_iCvarColor_tankstone = GetColor(sColorTankstone);
}

int GetColor(char[] sTemp)
{
	if( strcmp(sTemp, "") == 0 )
		return 0;

	char sColors[3][4];
	int color = ExplodeString(sTemp, " ", sColors, 3, 4);

	if( color != 3 )
		return 0;

	color = StringToInt(sColors[0]);
	color += 256 * StringToInt(sColors[1]);
	color += 65536 * StringToInt(sColors[2]);

	return color;
}

public void OnEntityCreated(int entity, const char[] classname)
{
	
	if(StrEqual(classname, "tank", true))
	{
		if(g_iCvarColor_tank > 0) //enable glow
		{
			SetEntProp(entity, Prop_Send, "m_iGlowType", 3);
			SetEntProp(entity, Prop_Send, "m_nGlowRange", 0);
			SetEntProp(entity, Prop_Send, "m_glowColorOverride", g_iCvarColor_tank);
		}	
	}

	
	if(StrEqual(classname, "witch", true))
	{
		if(g_iCvarColor_witch > 0) //enable glow
		{
			SetEntProp(entity, Prop_Send, "m_iGlowType", 3);
			SetEntProp(entity, Prop_Send, "m_nGlowRange", 0);
			SetEntProp(entity, Prop_Send, "m_glowColorOverride", g_iCvarColor_witch);
		}
	}

	if(StrEqual(classname, "tank_rock", true))
	{
		if(g_iCvarColor_tankstone > 0) //enable glow
		{
			SetEntProp(entity, Prop_Send, "m_iGlowType", 3);
			SetEntProp(entity, Prop_Send, "m_nGlowRange", 0);
			SetEntProp(entity, Prop_Send, "m_glowColorOverride", g_iCvarColor_tankstone);
		}
	}
}